package com.xkh.common.controller;

import com.xkh.common.common.constant.Result;
import com.xkh.common.util.FilesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author xkh
 * @date 2020/11/18 15:17
 */
@RestController
@RequestMapping("util/file")
@Api(tags = "文件模块")
public class FileController {


    @ApiOperation("文件内容追加")
    @PostMapping(value = "/fileAppender")
    @ApiImplicitParam(name = "path",value = "指定的文件路径")
    public Result fileAppender(String path) {
        return FilesUtil.fileAppender(path);
    }

    @ApiOperation("模板引擎封装")
    @PostMapping("/templatePackage")
    @ApiImplicitParam(name = "path",value = "存放生成的文件目录")
    public Result templatePackage(String path) {
        return FilesUtil.templatePackage(path);
    }

    @ApiOperation("单个文件本地上传")
    @PostMapping(value = "/localUpload")
    public Result localUpload(MultipartFile file) throws IOException {
        return FilesUtil.localUpload(file);
    }

    @ApiOperation("多个文件本地上传")
    @PostMapping(value = "/localUploads")
    public Result localUploads(List<MultipartFile> files) throws IOException {
        int i = 0;
        List<String> list = new ArrayList<>();

        for (MultipartFile file : files) {
            Result result = FilesUtil.localUpload(file);
            if (result.getCode().equals(200)) {
                list.add(result.getData().toString());
                i++;
            }
        }
        return Result.succeedData(list);
    }
}
